const app = getApp()
/*
 * fun [function] 需要防抖的函数
 * delay [number] 毫秒，防抖期限值
 */
function debounce(fun, delay = 300) {
  let timer;
  return function () {
    let ctx = this
    let args = arguments
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(() => {
      timer = null
      fun.apply(ctx, args)
    }, delay)
  }
}
/*
 * fun [function] 需要节流的函数
 * delay [number] 毫秒，节流期限值
 */
function throttle(fun, interval = 300) {
  let last;
  let timer;
  return function () {
    let ctx = this
    let args = arguments
    let now = new Date()
    if (last && now - last < interval) {
      clearTimeout(timer)
      timer = setTimeout(function () {
        last = now
        fun.apply(ctx, args)
      }, interval)
    } else {
      last = now
      fun.apply(ctx, args)
    }
  }
}

// 时间排序功能    点击升序降序
function isSort(type, arr) {
  if (type) {
    arr.sort((x, y) => {
      return Date.parse(x.update) - Date.parse(y.update)
    })
  } else {
    arr.sort((x, y) => {
      return Date.parse(y.update) - Date.parse(x.update)
    })
  }
}

//     搜索过滤
async function Filter(value,prevArr){
  const result = prevArr.filter(res =>{
    if(res.customer_name === null) return
    return res.customer_name.includes(value)
  })
  return result
}


//     拨打电话  
function callTel(phone){
  wx.makePhoneCall({
    phoneNumber: phone,
    fail: ()=>{
      return 0;
    }
  })
}

//   显示消息框
function toast(str,icon = 'none',delay = 1500){
  wx.showToast({
    title: str,
    icon,
    duration: delay,
    mask: true
  });
}

function ModalTitle(){
  wx.showModal({
    title: '提示',
    content: '今天是休息日，好好陪伴家人哦' ,
    showCancel:false ,
    success :_=>{}
  })
}


export {
  debounce,
  throttle,
  isSort,
  Filter,
  callTel,
  toast ,
  ModalTitle
}